/**
 * 
 */
package com.et114.modules.system.dictionary.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.CONST;
import com.et114.core.common.Error;
import com.et114.modules.org.message.OrgMessageManager;
import com.et114.modules.system.dictionary.form.BizTypeForm;
import com.et114.modules.system.dictionary.service.DictionaryFactory;
import com.et114.modules.system.dictionary.service.IDictionaryService;
import com.et114.modules.system.dictionary.vo.BizType;

import org.apache.commons.beanutils.PropertyUtils;

import java.util.List;

/*
 * @author guanhw
 */
public class DicTypeAction extends BaseDispatchAction {
	
	public  ActionForward loadDocTypes( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		BizTypeForm btForm = ( BizTypeForm )  form ; 
		BizType bt = new BizType ( ) ; 
		PropertyUtils.copyProperties ( bt , btForm ) ;
		
		IDictionaryService ds = DictionaryFactory.getInstansce ( ) ; 
		List bts = ds.retrieveBizTypeList ( bt ) ; 
		request.setAttribute ( "bts" , bts ) ;
		if ( bt.getBizTypeKey ( ) != null ) request.setAttribute ( "bizType___" , bt.getBizTypeKey ( ) ) ;
		return mapping.findForward ( "dics" ) ; 
	}
	
	
	
	public   ActionForward modDocType( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		IDictionaryService ds = DictionaryFactory.getInstansce ( ) ; 
		BizTypeForm btForm = ( BizTypeForm )  form ; 
		BizType bt = new BizType ( ) ; 
		PropertyUtils.copyProperties ( bt , btForm ) ;
		BizType type = ds.retrieveBizType(bt.getBizTypeId());
		if ( ! type.getBizTypeKey ( ).equals ( bt.getBizTypeKey ( ) ) ) {
			if ( ( ds.countBizType ( bt.getBizTypeKey ( ) ) ).longValue ( ) > 0 ) {
				Error error = new Error();
				error.setErrorTitle ( OrgMessageManager.get().getMessage("org.biztype.already.exist") ) ;
				request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
				return mapping.findForward( "glerror" ) ;
			}
		}
		boolean result = ds.validateType ( bt ) ; 
		if ( !result ) ds.saveBizType ( bt ) ; 
		else request.setAttribute ( "result" , "has" ) ;
		return mapping.findForward ( "glsuccess" ) ;
	}	
	
	
	public  ActionForward loadDocType( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		IDictionaryService ds = DictionaryFactory.getInstansce ( ) ; 
		BizTypeForm btForm = ( BizTypeForm )  form ; 
		String bizTypeId = btForm.getBizTypeId ( ) ; 
		if ( bizTypeId != null && !"".equals ( bizTypeId.trim ( ) ) ) {
			BizType bt = ds.retrieveBizType ( bizTypeId ) ;
			request.setAttribute ( "bizType" , bt ) ; 
		}
		return mapping.findForward ( "loadDicType" ) ; 
	}
	
	
	
	public  ActionForward newDocType( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		IDictionaryService ds = DictionaryFactory.getInstansce ( ) ; 
		BizTypeForm btForm = ( BizTypeForm )  form ; 
		BizType bt = new BizType ( ) ; 
		PropertyUtils.copyProperties ( bt , btForm ) ;
		if ( (ds.countBizType(bt.getBizTypeKey())).longValue() > 0 ) {
			Error error = new Error();
			error.setErrorTitle ( OrgMessageManager.get().getMessage("org.biztype.already.exist") ) ;
			request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
			return mapping.findForward( "glerror" ) ;
		}
		boolean result = ds.validateType ( bt ) ; 
		if ( !result ) ds.saveBizType ( bt ) ; 
		else request.setAttribute ( "result" , "has" ) ;		
		ds.saveBizType ( bt )  ; 
		return new ActionForward( "bizSuccess.go" , true ) ;
		//return mapping.findForward ( "glsuccess" ) ;
	}		
	
	
	public  ActionForward removeDocType( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		IDictionaryService ds = DictionaryFactory.getInstansce ( ) ; 
		BizTypeForm btForm = ( BizTypeForm )  form ; 
		Long[] deleteId = btForm.getDeleteId ( ) ; 
		if ( deleteId != null  ) {
			for ( int i = 0 ; i < deleteId.length ; i ++  ) {
				Long id = deleteId[ i ] ; 
				ds.deleteBizType ( String.valueOf ( id ) ) ; 
			}
		}  
		return new ActionForward(  "bizdicType.go?method=loadDocTypes" , true )   ;
	}		
}
